﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using SmartSpace.Core;
using System.IO;

namespace SmartSpace.Packaging
{
    public class XOrgUtil
    {
        public static List<Org> Load(string filePath)
        {
            XDocument xDoc = XDocument.Load(filePath);
            return Load(xDoc);
        }

        public static List<Org> Load(Stream stream)
        {
            XDocument xDoc = XDocument.Load(stream);
            return Load(xDoc);
        }

        private static List<Org> Load(XDocument xDoc)
        {
            List<Org> orgs = new List<Org>();
            IEnumerable<XElement> elements = xDoc.Root.Elements("org");
            foreach (var item in elements)
            {
                Org org = new Org();
                org.Id = new Guid(item.Attribute("id").Value);
                org.Code = item.Attribute("code").Value;
                org.Caption = item.Attribute("caption").Value;
                org.Type = Convert.ToInt16(item.Attribute("type").Value);
                org.SortIndex = Convert.ToInt32(item.Attribute("sort-index").Value);
                orgs.Add(org);
                Load(item, org);
            }
            return orgs;
        }

        private static void Load(XElement parentElement, Org parentOrg)
        {
            parentOrg.Children = new List<Org>();
            IEnumerable<XElement> elements = parentElement.Elements("org");
            foreach (var item in elements)
            {
                Org org = new Org();
                org.Id = new Guid(item.Attribute("id").Value);
                org.Code = item.Attribute("code").Value;
                org.Caption = item.Attribute("caption").Value;
                org.Type = Convert.ToInt16(item.Attribute("type").Value);
                org.SortIndex = Convert.ToInt32(item.Attribute("sort-index").Value);
                org.ParentId = parentOrg.Id;
                parentOrg.Children.Add(org);
                Load(item, org);
            }
        }

        public static bool Save(List<Org> orgs, string filePath)
        {
            XDocument xDoc = new XDocument();
            XElement root = new XElement("orgs");
            xDoc.Add(root);
            foreach (var item in orgs)
            {
                XElement element = new XElement("org",
                    new XAttribute("id", item.Id),
                    new XAttribute("code", item.Code),
                    new XAttribute("caption", item.Caption),
                    new XAttribute("type", item.Type),
                    new XAttribute("sort-index", item.SortIndex));
                root.Add(element);
                Save(element, item);
            }
            xDoc.Save(filePath);
            return true;
        }

        private static void Save(XElement parentElement, Org parentOrg)
        {
            if (parentOrg.Children != null)
            {
                foreach (var item in parentOrg.Children)
                {
                    XElement element = new XElement("org",
                        new XAttribute("id", item.Id),
                        new XAttribute("code", item.Code),
                        new XAttribute("caption", item.Caption),
                        new XAttribute("type", item.Type),
                        new XAttribute("sort-index", item.SortIndex));
                    parentElement.Add(element);

                    Save(element, item);
                }
            }
        }
    }
}
